#install.packages('ggplot2')
#library(ggplot2)

GOALS: Students should be able to use ggplot2 to generate publication quality graphics and understand and use the basics of the grammar of graphics.

##DataViz

Terminology:

First Plots with GGPLOT

#gapminder <- read.csv("https://goo.gl/BtBnPg", header = T)
gapminder <- read.csv('data/gapminder-FiveYearData.csv', header=T)

Let’s start off with an example:

library(ggplot2)
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

NOTE:

Alone the ggplot call isn’t enough to render the plot.

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
## If run, would produce a blank plot or error.
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Challenge 1

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-1

Challenge 2

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-2

Layers

ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
  geom_line()

ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
  geom_line() + geom_point()

ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country)) +
  geom_line(aes(color=continent)) + geom_point()

Challenge 3

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-3

Solution to challenge 3

Switch the order of the point and line layers from the previous example. What happened?

ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country)) +
 geom_point() + geom_line(aes(color=continent))

The lines now get drawn over the points!

Transformations and statistics

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp, color=continent)) +
  geom_point()

ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
  geom_point(alpha=0.5) + scale_y_log10()

ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + scale_x_log10() + geom_smooth(method="lm")

pwd <- ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() + scale_x_log10() + geom_smooth(method="lm", size=1.5)

pwd

Challenge 4a

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-3#challenge-4a

Challenge 4b

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-3#challenge-4b

Multi-panel figures: FACEting

starts.with <- substr(gapminder$country, start = 1, stop = 1)
az.countries <- gapminder[starts.with %in% c("A", "Z"), ]

Talk thru code: * We’ll start by subsetting the data using the substr function topull out a part of a character string; * in this case, the letters that occur in positions start through stop, inclusive, of the gapminder$country vector. * The operator %in% allows us to make multiple comparisons rather than write out long subsetting conditions (in this case, starts.with %in% c("A", "Z") is equivalent to starts.with == "A" | starts.with == "Z")

ggplot(data = az.countries, aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country)

Modifying text

Now lets clean up this figure for publication.

ggplot(data = az.countries, aes(x = year, y = lifeExp, color=continent)) +
  geom_line() + facet_wrap( ~ country) +
  labs(
    x = "Year",              # x axis title
    y = "Life expectancy",   # y axis title
    title = "Figure 1",      # main title of figure
    color = "Continent"      # title of legend
  ) +
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())

Challenge 5

http://swcarpentry.github.io/r-novice-gapminder/08-plot-ggplot2#challenge-5

How to save your plots

ggsave('~/path/to/figure/filename.png')
ggsave(plot1, file = "~/path/to/figure/filename.png")
ggsave(file = "/path/to/figure/filename.png", width = 6,
height =4)
ggsave(file = "/path/to/figure/filename.eps")
ggsave(file = "/path/to/figure/filename.jpg")
ggsave(file = "/path/to/figure/filename.pdf")

Resources:

This is just a taste of what you can do with ggplot2. RStudio provides a really useful cheat sheet of the different layers available, and more extensive documentation is available on the ggplot2 website. Finally, if you have no idea how to change something, a quick Google search will usually send you to a relevant question and answer on Stack Overflow with reusable code to modify!

ggplot cheat sheet https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf

ggplot site http://ggplot2.org/